/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Tp2Principal;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author Usager
 */
public class GererDamier2 extends MouseAdapter{

        int dimension = 8;
        Case[][] tabCases = new Case[dimension][dimension];
        boolean[][] caseRemplie = new boolean[dimension][dimension];
        boolean[][] caseDoubler = new boolean[dimension][dimension];
        boolean[][] caseDeplacer = new boolean[dimension][dimension];
        Case caseCourant;
        Fenetre fenetre;
        int action = 0; //lorsque l'utilisateur cliquera, ce sera sur le pion qu'il veut déplacer

        Case caseCliquee;
        int indexX;
        int indexY;
        boolean valide = false;

        @Override
        public void mouseClicked(MouseEvent me)
        {
            caseCourant = (Case) me.getSource();

            for (int i = 0; i < dimension; i++)
            {
                for (int j = 0; j < dimension; j++)
                {
                    if (caseCourant == tabCases[i][j])
                    {
                        indexX = i;
                        indexY = j;
                    }
                }
            }
            if (action == 0)
            {
                caseCliquee = caseCourant;

                if (caseRemplie[indexX][indexY]) //s'il y a un pion sur la case
                {
                    valide = true;
                }
                if (valide)
                {
                    try
                    {
                        action = 1;
                        if (indexX != 0)
                        {
                            caseDoubler[indexX - 1][indexY] = true;
                            if (indexY != 0)
                            {
                                caseDoubler[indexX - 1][indexY - 1] = true;
                                if (indexY != 0)
                                {
                                    caseDeplacer[indexX - 1][indexY - 2] = true;
                                }
                            }
                            if (indexY != (dimension - 1))
                            {
                                caseDoubler[indexX - 1][indexY + 1] = true;
                                if (indexY != (dimension-2))
                                {
                                    caseDeplacer[indexX - 1][indexY + 2] = true;
                                }
                            }
                            if (indexX != 1)
                            {
                                caseDeplacer[indexX - 2][indexY] = true;
                                if (indexY != 0)
                                {
                                    caseDeplacer[indexX - 2][indexY - 1] = true;
                                    if (indexY != 1)
                                    {
                                        caseDeplacer[indexX - 2][indexY - 2] = true;
                                    }
                                }
                                if (indexY != (dimension - 1))
                                {
                                    caseDeplacer[indexX - 2][indexY + 1] = true;
                                    if (indexY != (dimension-2))
                                    {
                                        caseDeplacer[indexX - 2][indexY + 2] = true;
                                    }
                                }
                            }
                        }
                        if (indexX != (dimension - 1))
                        {
                            caseDoubler[indexX + 1][indexY] = true;
                            if (indexY != 0)
                            {
                                caseDoubler[indexX + 1][indexY - 1] = true;
                                if (indexY != 1)
                                {
                                    caseDeplacer[indexX + 1][indexY - 2] = true;
                                }
                            }
                            if (indexY != (dimension - 1))
                            {
                                caseDoubler[indexX + 1][indexY + 1] = true;
                                if (indexY != (dimension-2))
                                {
                                    caseDeplacer[indexX + 1][indexY + 2] = true;
                                }
                            }
                            if (indexX != (dimension-2))
                            {
                                caseDeplacer[indexX + 2][indexY] = true;
                                if (indexY != 0)
                                {
                                    caseDeplacer[indexX + 2][indexY - 1] = true;
                                    if (indexY != 1)
                                    {
                                        caseDeplacer[indexX + 2][indexY - 2] = true;
                                    }
                                }
                                if (indexY != (dimension - 1))
                                {
                                    caseDeplacer[indexX + 2][indexY + 1] = true;
                                    if (indexY != (dimension-2))
                                    {
                                        caseDeplacer[indexX + 2][indexY + 2] = true;
                                    }
                                }
                            }
                        }
                        if (indexY != 0)
                        {
                            caseDoubler[indexX][indexY - 1] = true;
                            if(indexY!=1)
                            {
                                caseDeplacer[indexX][indexY - 2] = true;
                            }
                        }
                        if (indexY != (dimension - 1))
                        {
                            caseDoubler[indexX][indexY + 1] = true;
                            if(indexY!=(dimension-2))
                            {
                                caseDeplacer[indexX][indexY + 2] = true;
                            }
                        }


                    }
                    catch (Exception e)
                    {
                        System.out.println("Erreur!");
                    }
                }

            }
            else //action=1
            {
                if (caseDoubler[indexX][indexY])
                {
                    action = 0;
                    //caseCourant.add(new Pion(Color.RED));
                    fenetre.getRootPane().revalidate();
                    fenetre.repaint();
                }
                else if (caseDeplacer[indexX][indexY])
                {
                    action = 0;
                    //caseCourant.add(new Pion(Color.RED));
                    caseCliquee.removeAll();
                    fenetre.getRootPane().revalidate();
                    fenetre.repaint();
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Vous ne pouvez pas " +
                            "déplacer le pion à cet endroit.");
                }

            }
        }
    }


